package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyMQConfig {

    /**
     * 容器中的binding,queue,exchange,spring都会自动创建(前提是rabbitMq没有的情况下,如果已经有了,就只能在rabbitmq那里手动删除)
     **/
    @Bean
    public Queue orderDelayQueue() {

        /**
         * 设置相关属性
         * x-dead-letter-exchange: order-event-exchange(消息过期后要交给指定的exchange)
         * x-dead-letter-routing-key: order.release.order(消息过期后根据指定的routing-key进去队列)
         * x-message-ttl : 60000(过期时间)
         **/
        Map<String, Object> map = new HashMap();
        map.put("x-dead-letter-exchange", "order-event-exchange");
        map.put("x-dead-letter-routing-key", "order.release.order");
        map.put("x-message-ttl", 600000);

        //String name(队列名字), boolean durable(是否持久化), boolean exclusive(是不是排他的), boolean autoDelete(是否自动删除), Map<String, Object> arguments(设置一些参数,比如死信队列等相关参数)
        Queue queue = new Queue("order.delay.queue", true, false, false, map);
        return queue;
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }

    @Bean
    public Exchange orderEventExchange() {
        //String name(交换机名字), boolean durable(是否持久化), boolean autoDelete(是否自动删除), Map<String, Object> arguments(设置一些自定义参数,详细可以参考官方文档)
        return new TopicExchange("order-event-exchange", true, false);
    }

    @Bean
    public Binding orderCreateOrderBingding() {
        //String destination(目的地), Binding.DestinationType destinationType(绑定的类型), String exchange(要绑定的交换机), String routingKey(绑定时指定的路由键), Map<String, Object> arguments(设置一些自定义参数)
        return new Binding("order.delay.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.create.order", null);
    }

    @Bean
    public Binding orderReleaseOrderBingding() {
        return new Binding("order.release.order.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.order", null);
    }

    /**
     * 订单释放直接和库存释放进行绑定
     **/
    @Bean
    public Binding orderReleaseOtherBingding() {
        return new Binding("order.release.stock.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.order.#", null);
    }

    @Bean
    public Queue orderSeckillOrderQueue() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        return new Queue("order.seckill.order.queue", true, false, false);
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding() {
        //String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
        return new Binding("order.seckill.order.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.seckill.order", null);
    }
}
